package com.neusoft.databus.core.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.exception.CustomRuntimeException;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.databus.common.dto.MetricGroupDTO;
import com.neusoft.databus.core.service.MetricGroupService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 指标组控制器
 *
 * @author naxiang
 */
@Api("指标组控制器")
@RestController
@RequestMapping(value = "/api/w/metricgroups")
public class MetricGroupController {
    @Autowired
    private MetricGroupService metricGroupService;

    /**
     * 查询所有指标组 <br>
     * GET <br>
     *
     * @return ResultListDTO<MetricGroupDTO>
     */
    @ApiOperation("查询所有指标组")
    @GetMapping
    public ResultListDTO<MetricGroupDTO> index(final Searchable searchable) {
        final List<MetricGroupDTO> DTOs = this.metricGroupService.search(searchable);
        return ResultListDTO.success(DTOs);
    }

    /**
     * 分页查询所有指标组 <br>
     * GET <br>
     *
     * @param searchable 查询对象
     * @param pageable 分页对象
     * @return PageResultDTO<MetricGroupDTO>
     */
    @ApiOperation("分页查询所有指标组")
    @GetMapping("/p")
    public PageResultDTO<MetricGroupDTO> search(final Searchable searchable, final Pageable pageable) {
        final Page<MetricGroupDTO> DTOs = this.metricGroupService.search(pageable, searchable);
        return PageResultDTO.success(DTOs);
    }

    @ApiOperation("查询单个指标组")
    @GetMapping("/{code}")
    public ResultDTO<MetricGroupDTO> searchByCode(@PathVariable final String code) {
        final MetricGroupDTO dto = this.metricGroupService.findByCode(code);
        return ResultDTO.success(dto);
    }

    /**
     * 创建指标组 <br>
     * POST <br>
     *
     * @param dto MetricGroupDTO
     * @return ResultDTO<MetricGroupDTO>
     */
    @ApiOperation("创建指标组 ")
    @PostMapping
    public ResultDTO<MetricGroupDTO> create(@RequestBody final MetricGroupDTO dto) {
        final MetricGroupDTO result = this.metricGroupService.create(dto);
        return ResultDTO.success(result);
    }

    /**
     * 删除指标组，物理删除 <br>
     * DELETE <br>
     *
     * @param code 指标组编码
     * @return ResultDTO<Void>
     * @throws CustomRuntimeException
     */
    @ApiOperation("删除指标组，物理删除")
    @DeleteMapping("/{code}")
    public ResultDTO<Void> deleteByCode(@PathVariable final String code) {
        this.metricGroupService.deleteByCode(code);
        return ResultDTO.success();
    }

    /**
     * 删除指标组（內部使用）
     *
     * @param id 指标组ID
     * @return ResultDTO<Void>
     * @throws CustomRuntimeException
     */
    @ApiOperation("删除指标组（內部使用）")
    @DeleteMapping("/{id}/deleteById")
    public ResultDTO<Void> deleteById(@PathVariable final Long id) {
        this.metricGroupService.deleteById(id);
        return ResultDTO.success();
    }

    /**
     * 指标组绑定指标（外部使用）
     *
     * @param code 指标组编码
     * @param metricCodeList 指标编码列表
     * @return ResultDTO<Void>
     * @throws CustomRuntimeException
     */
    @ApiOperation("指标组绑定指标（外部使用）")
    @PutMapping("/{code}/bind")
    public ResultDTO<MetricGroupDTO> bindByCode(@PathVariable final String code,
            @RequestBody final List<String> metricCodeList) {
        final MetricGroupDTO result = this.metricGroupService.bindByCode(code, metricCodeList);
        return ResultDTO.success(result);
    }

    /**
     * 指标组绑定指标（內部使用）
     *
     * @param code 指标组编码
     * @param metricIdList 指标ID列表
     * @return ResultDTO<Void>
     * @throws CustomRuntimeException
     */
    @ApiOperation("指标组绑定指标（內部使用）")
    @PutMapping("/{id}/bindById")
    public ResultDTO<?> bindById(@PathVariable final Long id, @RequestBody final List<Long> metricIdList) {
        this.metricGroupService.bindById(id, metricIdList);
        return ResultDTO.success();
    }

    /**
     * 更新指标组
     *
     * @param code 指标组编码
     * @return ResultDTO<Void>
     * @throws CustomRuntimeException
     */
    @ApiOperation("更新指标组")
    @PutMapping("/{code}")
    public ResultDTO<?> update(@PathVariable final String code, @RequestBody final MetricGroupDTO dto) {
        final MetricGroupDTO result = this.metricGroupService.update(code, dto);
        return ResultDTO.success(result);
    }

    /**
     * 指标组导出
     *
     * @param file
     * @return
     */
    @ApiOperation("指标组导出")
    @GetMapping("/export")
    public void export(final Searchable searchable, final Pageable pageable, final HttpServletResponse response) {
        this.metricGroupService.export(searchable, pageable, response);
    }
}
